package com.Sake.web.Service.Home.impl;

import com.Sake.common.constant.Constants;
import com.Sake.common.constant.ReturnMsg;
import com.Sake.common.utils.result.ResultUtil;
import com.Sake.web.Service.Home.HomeMessageService;
import com.Sake.web.mapper.HomeMessageMapper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zsh
 * @date 2021.7.6
 */
@Service
public class HomeMessageServiceImpl implements HomeMessageService {


    @Autowired
    private ResultUtil resultUtil;

    @Autowired
    private HomeMessageMapper homeMessageMapper;

    /**
     * 获取所有主页资料
     *
     * @return 资料
     */
    @Override
    public JSONObject getHomeMessage() {
        JSONObject homeMessage = homeMessageMapper.getHomeMessage();
        return resultUtil.resultSuccessData(homeMessage);
    }

    /**
     * 修改主页资料
     *
     * @param head_name               主页昵称
     * @param personalDeclaration     个人声明
     * @param bottomInformation       底部信息
     * @param login_bottomInformation 登录页底部信息
     * @return 修改状态
     */
    @Override
    public JSONObject modifyHomepageInformation(String head_name, String personalDeclaration, String bottomInformation, String login_bottomInformation) {
        long timeMillis = System.currentTimeMillis();
        int i = homeMessageMapper.modifyHomepageInformation(head_name, personalDeclaration, bottomInformation, login_bottomInformation, timeMillis);
        if (i == 1) {
            return resultUtil.resultSuccess();
        }
        return resultUtil.resultCustomMsg(ReturnMsg.FAILURE, "修改资料失败");
    }

    /**
     * 获取动态
     *
     * @return 动态
     */
    @Override
    public JSONObject getHomeDynamic() {
        JSONArray array = homeMessageMapper.getHomeDynamic();
        JSONArray result = new JSONArray();
        array.stream().forEach(str -> {
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(str));
            String contentImages = jsonObject.getString("contentImages");
            if (contentImages.split(Constants.DELIMITER).length > 1) {
                jsonObject.put("imagesLength", contentImages.split(Constants.DELIMITER).length);
                jsonObject.put("images", contentImages.split(Constants.DELIMITER));
            }
            result.add(jsonObject);
        });
        return resultUtil.resultSuccessData(result);
    }

    /**
     * 添加动态
     *
     * @param userid        登录用户id
     * @param ip            登录请求地址ip
     * @param content       内容文本
     * @param contentImages 配图地址  xxx,xxx,xxx
     * @return 状态
     */
    @Override
    public JSONObject initiateADynamic(int userid, String ip, String content, String contentImages) {
        //当前时间
        long timeMillis = System.currentTimeMillis();
       int i= homeMessageMapper.initiateADynamic(userid,ip,content,contentImages,timeMillis);
       if (i==1){
           return resultUtil.resultSuccess();
       }
       return resultUtil.resultCustomMsg(ReturnMsg.PARAMETER_EXC,"未知错误");
    }
}
